class Container < ActiveRecord::Base
  belongs_to  :yarn
  belongs_to  :container_type
  belongs_to  :container_state
  belongs_to  :supplier
  belongs_to  :roll
  belongs_to  :loom
  belongs_to  :load_type
  
  validates_uniqueness_of :code
  validates_presence_of :code, :supplier, :container_type, :net_weight, :yarn
  
  def before_create
    self.container_state = ContainerState.find_by_code(0)
  end
  
  def validate
    if net_weight >= gross_weight
      errors.add(:net_weight,"should be LESS THAN the net weight.") unless gross_weight.nil?
    end
  end
  
  def is_inventory
    self.container_state.code == 0
  end
  
  def is_production
    self.container_state.code == 1
  end
  
  def is_consumed
    self.container_state.code == 2
  end
  
  
end
